package com.mlan.userap.adapter;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.mlan.userap.AppConfig;
import com.mlan.userap.R;
import com.mlan.userap.component.FaceManager;
import com.mlan.userap.component.MsgListener;
import com.mlan.userap.model.ChatGroupInfo;
import com.mlan.userap.model.MsgEntity;
import com.mlan.userap.untils.ImageLoader;
import com.mlan.userap.untils.LogHelper;
import com.mlan.userap.untils.ScreenUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

public class ChatMsgAdapter  extends RecyclerView.Adapter<RecyclerView.ViewHolder>  {

    public static final Pattern EMOTION_URL = Pattern.compile("\\[(\\S+?)\\]");
    Context mContext;
    LayoutInflater mInflater;
    List<MsgEntity> msgList = new ArrayList<MsgEntity>();

    private Map<String, String> msgMap = new HashMap<>();
    private MsgListener msgListener;

    private int groupType;

    public ChatMsgAdapter(Context context,int groupType) {
        this.mInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.groupType = groupType;
    }

    public void setListener(MsgListener mItemEventListener) {
        this.msgListener = mItemEventListener;
    }

    public List<MsgEntity> getList() {
        return msgList;
    }

//    public void addList(List<MsgEntity> models) {
//        for(int i=0;i<models.size();i++){
//            this.msgList.add(i,models.get(i));
//        }
//
//        notifyDataSetChanged();
//    }

    public void insertEntity(MsgEntity msg) {
        if(!msgMap.containsKey(msg.getMsgId())) {
            msgMap.put(msg.getMsgId(), "1");
            this.msgList.add(msg);
            Collections.sort(this.msgList);
            int newPosition = this.msgList.indexOf(msg);
            notifyItemChanged(newPosition);

        }
    }

    public void addEntity(MsgEntity msg) {
        if(!msgMap.containsKey(msg.getMsgId())){
            msgMap.put(msg.getMsgId(),"1");
            int position = this.msgList.size();
            this.msgList.add(msg);
            notifyItemInserted(position);
        }
    }
    /**
     *
     * @param parent
     * @param viewType  1:文本 2:图片  3:语音 4:视屏 5:文件 6:表情  7:音乐
     * @return
     */
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder=null;
        switch (viewType) {
            case 2:
                viewHolder =new ViewHolderImg(LayoutInflater.from(mContext).inflate(R.layout.item_msg_img, parent, false));
                break;
            case 31:
                viewHolder =new ViewHolderAudio(LayoutInflater.from(mContext).inflate(R.layout.item_msg_audio, parent, false));
                break;
            case 32:
                viewHolder =new ViewHolderAudio(LayoutInflater.from(mContext).inflate(R.layout.item_msg_audio_my, parent, false));
                break;
            case 5:
                viewHolder =new ViewHolderFile(LayoutInflater.from(mContext).inflate(R.layout.item_msg_file, parent, false));
                break;
            case 4:
                viewHolder =new ViewHolderVedio(LayoutInflater.from(mContext).inflate(R.layout.item_msg_vedio, parent, false));
                break;

            default:
                viewHolder =new ViewHolderTxt(LayoutInflater.from(mContext).inflate(R.layout.item_msg_text, parent, false));
                break;
        }
        return viewHolder;
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        MsgEntity msginfo = msgList.get(position);
        //是接收放
        boolean isReceive = msginfo.getUidFrom()!= AppConfig.userId;
        if(holder.getClass()==ViewHolderTxt.class){
            ViewHolderTxt holdTxt = (ViewHolderTxt) holder;
            holdTxt.iv_head_left.setVisibility(isReceive? View.VISIBLE:View.GONE);
            holdTxt.iv_head_right.setVisibility(isReceive?View.GONE:View.VISIBLE);
            String message = msginfo.getMsgContent();
            if(message==null) message="";
            if(!message.isEmpty()){
                message=message.replace("<br>","\n");
                FaceManager.getInstance().emojiJudge(holdTxt.tv_content,message);

            }

            if (isReceive) {
                ImageLoader.show(holdTxt.iv_head_left, msginfo.getHeadFrom());
                setGravity(holdTxt.tv_content, Gravity.START);
                holdTxt.tv_content.setBackgroundResource(R.drawable.bzp);
                if(groupType==1) {
                    holdTxt.tv_nick.setText("haha");
                    holdTxt.tv_nick.setVisibility(View.VISIBLE);
                    FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) holdTxt.tv_content.getLayoutParams();
                    params.topMargin = ScreenUtil.dip2px(22);
                    holdTxt.tv_content.setLayoutParams(params);
                }

            } else {
                ImageLoader.show(holdTxt.iv_head_right, msginfo.getHeadFrom());
                setGravity(holdTxt.tv_content, Gravity.END);
                holdTxt.tv_content.setBackgroundResource(R.drawable.bzu);
                if(groupType==1) {
                    holdTxt.tv_nick.setVisibility(View.GONE);
                    FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) holdTxt.tv_content.getLayoutParams();
                    params.topMargin = 0;
                    holdTxt.tv_content.setLayoutParams(params);
                }
            }
            holdTxt.tv_content.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (msgListener != null) {
                        msgListener.onClick(v, msginfo);
                    }
                }
            });
            holdTxt.tv_content.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if (msgListener != null) {
                        msgListener.onLongClick(v, msginfo);
                    }
                    return true;
                }
            });
        }
        if(holder.getClass()==ViewHolderImg.class) {
            ViewHolderImg holdImage = (ViewHolderImg) holder;
            holdImage.iv_head_left.setVisibility(isReceive?View.VISIBLE:View.GONE);
            holdImage.iv_head_right.setVisibility(isReceive?View.GONE:View.VISIBLE);

            if(msginfo.getFileInfo().getThumimg().length()>0){
                LogHelper.i("显示："+msginfo.getFileInfo().getThumimg());
                ImageLoader.show(holdImage.iv_img, msginfo.getFileInfo().getThumimg());
            }
            else{
                ImageLoader.show(holdImage.iv_img, msginfo.getFileInfo().getResurl());
            }

            if (isReceive) {
                ImageLoader.show(holdImage.iv_head_left, msginfo.getHeadFrom());
                setGravity(holdImage.iv_img, Gravity.START);
            } else {
                ImageLoader.show(holdImage.iv_head_right, msginfo.getHeadFrom());
                setGravity(holdImage.iv_img, Gravity.END);
            }
            holdImage.iv_img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (msgListener != null) {
                        msgListener.onClick(v, msginfo);
                    }
                }
            });
            holdImage.iv_img.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if (msgListener != null) {
                        msgListener.onLongClick(v, msginfo);
                    }
                    return true;
                }
            });
        }
        if(holder.getClass()==ViewHolderFile.class) {
            ViewHolderFile holdFile = (ViewHolderFile) holder;
//            isReceive=false;
            holdFile.iv_head_left.setVisibility(isReceive?View.VISIBLE:View.GONE);
            holdFile.iv_head_right.setVisibility(isReceive?View.GONE:View.VISIBLE);
            holdFile.item_file_name.setText(msginfo.getFileInfo().getResname());
          //  holdFile.item_file_size.setText(msginfo.getFileInfo().getDuration());
            if (isReceive) {
                ImageLoader.show(holdFile.item_file_icon_left, msginfo.getFileInfo().getThumimg());
                ImageLoader.show(holdFile.iv_head_left, msginfo.getHeadFrom());
                setGravity(holdFile.ll_file, Gravity.START);
                holdFile.item_file_icon_right.setVisibility(View.GONE);
                holdFile.item_file_icon_left.setVisibility(View.VISIBLE);
            } else {
                ImageLoader.show(holdFile.item_file_icon_right, msginfo.getFileInfo().getThumimg());
                ImageLoader.show(holdFile.iv_head_right, msginfo.getHeadFrom());
                setGravity(holdFile.ll_file, Gravity.END);
//                RelativeLayout.LayoutParams layoutParams =(RelativeLayout.LayoutParams)holdFile.item_file_size.getLayoutParams();
//                layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
//                holdFile.item_file_size.setLayoutParams(layoutParams);
                holdFile.item_file_icon_left.setVisibility(View.GONE);
                holdFile.item_file_icon_right.setVisibility(View.VISIBLE);
            }
            holdFile.ll_file.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (msgListener != null) {
                        msgListener.onClick(v, msginfo);
                    }
                }
            });
            holdFile.ll_file.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if (msgListener != null) {
                        msgListener.onLongClick(v, msginfo);
                    }
                    return true;
                }
            });
        }

        if(holder.getClass()==ViewHolderVedio.class) {
            ViewHolderVedio holderVedio = (ViewHolderVedio) holder;

            holderVedio.iv_head_left.setVisibility(isReceive?View.VISIBLE:View.GONE);
            holderVedio.iv_head_right.setVisibility(isReceive?View.GONE:View.VISIBLE);
           // holderVedio.item_vedio_duration.setText(msginfo.getFileInfo().getDuration());
            ImageLoader.show(holderVedio.iv_thumimg, msginfo.getFileInfo().getThumimg());
            if (isReceive) {
                ImageLoader.show(holderVedio.iv_head_left,  msginfo.getHeadFrom());
                setGravity(holderVedio.rl_vedio, Gravity.START);

            } else {
                ImageLoader.show(holderVedio.iv_head_right,  msginfo.getHeadFrom());
                setGravity(holderVedio.rl_vedio, Gravity.END);

            }
            holderVedio.iv_thumimg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (msgListener != null) {
                        msgListener.onClick(v, msginfo);
                    }
                }
            });
            holderVedio.iv_thumimg.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if (msgListener != null) {
                        msgListener.onLongClick(v, msginfo);
                    }
                    return true;
                }
            });
        }

        if(holder.getClass()==ViewHolderAudio.class) {
            ViewHolderAudio holderAudio = (ViewHolderAudio) holder;
            if (msginfo.getFileInfo()==null){
                return;
            }
            long swidth = 70+msginfo.getFileInfo().getTime()*7;
            if(swidth>270) swidth=270;
            FrameLayout.LayoutParams params2 = (FrameLayout.LayoutParams) holderAudio.ll_audio.getLayoutParams();
            params2.width =ScreenUtil.dip2px(swidth);
            holderAudio.ll_audio.setLayoutParams(params2);

            holderAudio.item_audio_time.setText(msginfo.getFileInfo().getTime()+"''");

            ImageLoader.show(holderAudio.iv_head,  msginfo.getHeadFrom());
            holderAudio.ani_play.setVisibility(View.GONE);

            holderAudio.ll_audio.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (msgListener != null) {
                        msgListener.onClick(holderAudio.itemLayout,msginfo);
                    }
                }
            });

        }
    }



    private void setGravity(View view , int gravity ) {
        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) view.getLayoutParams();
        params.gravity = gravity;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    @Override
    public int getItemCount() {
        return msgList == null ? 0 : msgList.size() ;
    }

    public MsgEntity getItem(int index) {
        return msgList.get(index);
    }

    @Override
    public int getItemViewType(int position) {
        int viewType=0;
        if(position<msgList.size()){
            viewType = msgList.get(position).getMsgType();
            if(viewType==3) {
                viewType = msgList.get(position).getUidFrom()!= AppConfig.userId?31:32;
            }
        }
        return viewType;
    }

    public interface ItemEventListener {
        void onItemClick(MsgEntity appData, View v, int position);
    }



    class ViewHolderTxt extends RecyclerView.ViewHolder {
        RelativeLayout rl_container;
        ImageView iv_head_left;
        ImageView iv_head_right;
        TextView tv_content;
        TextView tv_nick;

        ViewHolderTxt(View itemView) {
            super(itemView);
            rl_container = itemView.findViewById(R.id.rl_container);
            iv_head_left = itemView.findViewById(R.id.iv_head_left);
            iv_head_right = itemView.findViewById(R.id.iv_head_right);
            tv_content= itemView.findViewById(R.id.tv_content);
            tv_nick= itemView.findViewById(R.id.tv_nick);
        }
    }

    class ViewHolderImg extends RecyclerView.ViewHolder {
        RelativeLayout rl_container;
        ImageView iv_head_left;
        ImageView iv_head_right;
        ImageView iv_img;
        ViewHolderImg(View itemView) {
            super(itemView);
            rl_container = itemView.findViewById(R.id.rl_container);
            iv_head_left = itemView.findViewById(R.id.iv_head_left);
            iv_head_right = itemView.findViewById(R.id.iv_head_right);
            iv_img= itemView.findViewById(R.id.iv_img);
        }
    }
    class ViewHolderFile extends RecyclerView.ViewHolder {
        RelativeLayout rl_container;
        ImageView iv_head_left;
        ImageView iv_head_right;
        LinearLayout ll_file;
        ImageView item_file_icon_left;
        ImageView item_file_icon_right;
        TextView item_file_name;
        TextView item_file_size;
        ViewHolderFile(View itemView) {
            super(itemView);
            rl_container = itemView.findViewById(R.id.rl_container);
            iv_head_left = itemView.findViewById(R.id.iv_head_left);
            iv_head_right = itemView.findViewById(R.id.iv_head_right);
            ll_file = itemView.findViewById(R.id.tv_file);
            item_file_icon_left = itemView.findViewById(R.id.item_file_icon_left);
            item_file_icon_right = itemView.findViewById(R.id.item_file_icon_right);
            item_file_name = itemView.findViewById(R.id.item_file_name);
            item_file_size = itemView.findViewById(R.id.item_file_size);
        }
    }
    class ViewHolderVedio extends RecyclerView.ViewHolder {
        RelativeLayout rl_container;
        ImageView iv_head_left;
        ImageView iv_head_right;
        RelativeLayout rl_vedio;
        ImageView iv_thumimg;
        TextView item_vedio_duration;
        ViewHolderVedio(View itemView) {
            super(itemView);
            rl_container = itemView.findViewById(R.id.rl_container);
            iv_head_left = itemView.findViewById(R.id.iv_head_left);
            iv_head_right = itemView.findViewById(R.id.iv_head_right);
            rl_vedio = itemView.findViewById(R.id.rl_vedio);
            iv_thumimg = itemView.findViewById(R.id.iv_thumimg);
            item_vedio_duration = itemView.findViewById(R.id.item_vedio_duration);

        }
    }
    class ViewHolderAudio extends RecyclerView.ViewHolder {
        RelativeLayout rl_container;
        ImageView iv_head;
        LinearLayout ll_audio;
        ProgressBar ani_play;
        ImageView item_autoimg;
        TextView item_audio_time;
        View itemLayout;

        ViewHolderAudio(View itemView) {
            super(itemView);
            rl_container = itemView.findViewById(R.id.rl_container);
            iv_head = itemView.findViewById(R.id.iv_head);
            ll_audio = itemView.findViewById(R.id.ll_audio);
            ani_play = itemView.findViewById(R.id.ani_play);
            item_autoimg = itemView.findViewById(R.id.item_autoimg);
            item_audio_time = itemView.findViewById(R.id.item_audio_time);
            itemLayout=itemView;
        }
    }
}
